package cst.a110lyj.fragmenttest110lyj.model;

import android.widget.Toast;

import java.util.List;

import cst.a110lyj.fragmenttest110lyj.bean.ArticleBean;
import cst.a110lyj.fragmenttest110lyj.bean.UserBean;
import cst.a110lyj.fragmenttest110lyj.iface.AmicoolIface;
import cst.a110lyj.fragmenttest110lyj.iface.AmicoolListener;
import cst.a110lyj.fragmenttest110lyj.service.ResService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by admin on 2017/6/8/008.
 */

public class AmicoolModel implements AmicoolIface{
    private Retrofit retrofit;
    private ResService service;
    private static final String BASE_URL = "http://amicool.neusoft.edu.cn/";//主机地址
    public AmicoolModel(String type) {
        if(type .equals("gson") ){
            retrofit=new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        else {
            retrofit=new Retrofit
                    .Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        service=retrofit.create(ResService.class);

    }
    @Override
    public void login(String username, String password, final  AmicoolListener l) {
        Call<UserBean> call =service.login(username, password);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {

            }
        });
    }

    @Override
    public void reg(String username, String password, String tel, final AmicoolListener l) {
       Call<String>  call = service.reg(username, password, tel);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                 l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getList(String mod, int page, String sessionID, final  AmicoolListener l) {

        Call<List<ArticleBean>> call = service.getArticleList(mod, page, sessionID);
        switch (mod){
            case "article":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;

            case "video":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "tware":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "tcase":
                call.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;
            case "video2":
                Call<List<ArticleBean>> call2 = service.getVideo2("video",page,sessionID);
                call2.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });

                break;
            case "project":
                Call<List<ArticleBean>> project = service.getProject(mod,page,sessionID);
                project.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });

                break;
        }



    }



    @Override
    public void getDetail(String mod, int id, String s,final AmicoolListener l) {
    }



}
